import React, { useRef } from "react";
import { Modal } from "antd";
import { useDispatch, useSelector } from "react-redux";
import { Dispatch, RootState } from "@/store";
import CommonForm from "./Form/CommonForm";

interface globalModal {
  data?: any;
}
const componentsMap = {
  common: CommonForm,
};
const GlobalModal = (props: globalModal) => {
  const dispatch = useDispatch<Dispatch>();
  const { modalConfig } = useSelector((state: RootState) => state.diagram);
  const formRef = useRef<{ submit: any }>(null);

  const handleOk = () => {
    formRef.current?.submit().then(() => {
      dispatch.diagram.closeModal();
    });
  };

  const handleCancel = () => {
    dispatch.diagram.closeModal();
  };

  const Component = componentsMap["common"];

  return (
    <Modal
      title="编辑节点"
      visible={modalConfig?.visible}
      onOk={handleOk}
      onCancel={handleCancel}
    >
      {Component && <Component ref={formRef} />}
    </Modal>
  );
};
export default GlobalModal;
